package sisi.agents;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * PortsagentsController.java - Controller class for the <i>Ports Agents database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author SISI Software  - www.sisisoftware.it
 * @version 1.0, 19/03/2015
 * 
 */
public class PortsagentsController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Portsagent[] get(String cerca) {
		List<Portsagent> lista = getList(cerca);
			return lista.toArray(new Portsagent[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Portsagent> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.agentname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portsagent as c " + cWhere 
					+ " order by c.id ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Portsagent> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<Portsagent> getListxIdPort(Integer idport) {

		String cWhere = "";

		cWhere = " WHERE c.portid="+idport;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portsagent as c " + cWhere 
					+ " order by c.id ");

			List<Portsagent> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Portsagent> getListxIdAgent(Integer idAgent) {

		String cWhere = "";

		cWhere = " WHERE c.agentid="+idAgent;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portsagent as c " + cWhere 
					+ " order by c.seq ");

			List<Portsagent> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Portsagent getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Portsagent portsagentx = em.find(Portsagent.class, id);
			return portsagentx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Portsagent add(Portsagent portsagent) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(portsagent);
			em.getTransaction().commit();
			portsagent = refresh(portsagent);
		} finally {
			em.close();
			return portsagent;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Portsagent portsagent) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Portsagent portsagentx = em.find(Portsagent.class, portsagent.getId());
			portsagentx.setAgentid(portsagent.getAgentid());
			portsagentx.setAgentname(portsagent.getAgentname());
			portsagentx.setCountry(portsagent.getCountry());
			portsagentx.setPortid(portsagent.getPortid());
			portsagentx.setPortname(portsagent.getPortname());
			portsagentx.setSeq(portsagent.getSeq());
				
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Portsagent refresh(Portsagent portsagent) {

		Portsagent portsagentx = portsagent;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			portsagentx = em.find(Portsagent.class, portsagent.getId());
			em.flush();
			em.refresh(portsagentx);
		} finally {
			em.close();
			return portsagentx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Portsagent portsagent) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Portsagent agentx = em.find(Portsagent.class, portsagent.getId());
			em.remove(agentx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
